package io.renren.modules.app.message.handler;

import io.renren.modules.app.message.MessageResult;
import io.renren.modules.app.message.dto.JdVopMessageReadEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author :
 * @version V1.0
 * @Project:
 * @Package
 * @Description:
 * @date Date :
 **/
@Slf4j
public abstract class AbstractJdVopMessageHandler implements JdVopMessageHandler {

    private AtomicInteger msgTotal;
    private String beanName;
    private  final ApplicationContext applicationContext;
    AbstractJdVopMessageHandler(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    @Override
    public void read(MessageResult msg) {
        log.info("JD_VOP MSG UNTREATED:{}",msg);
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    @Override
    public void batchRead(List<MessageResult> msgs) {
        msgs.parallelStream().forEach(this::read);
    }
    @Override
    public int decrement() {
        return msgTotal.getAndDecrement();
    }

    @Override
    public void setMsgTotal(int total) {
        msgTotal=new AtomicInteger(total);
    }

    @Override
    public int getMsgTotal() {
        if(msgTotal==null) msgTotal=new AtomicInteger(0);
        return msgTotal.get();
    }

    @Override
    public void notifyMsgReadEvent(MessageResult msg,boolean flag) {
        applicationContext.publishEvent(new JdVopMessageReadEvent(this,msg.getId(),flag,beanName));
    }
}
